<?php
/**
 * 冲突申诉模型
 * 
 * @author		maxuebing<276654894@qq.com>
 * @copyright	Copyright (c) , Chinasofti, Inc.
 */
class salesDisputeModel extends RelationModel {

	public function __construct() {
		parent::__construct();
		
		$this->user = getUserSession();
	}
	
	/**
	 * 获得状态信息
	 * 
	 */
	public function getProgress($status, $createUserId, $createRealName, $toUserId, $toRealName){
		$progress = '处理中'	;
		
		$user = getUserSession();
		
		//	0 => '待原录入者确认',
		if($status == 0){
			if($user['id'] == $toUserId){
				$progress = "待我确认";
			}else{
				$progress = "待原录入者{$toRealName}确认";
			}
		}
		//	1 => '原始录入者已知晓，待处理',
		if($status == 1){
			if($user['id'] == $toUserId){
				$progress = "我已知晓，未处理";
			}else{
				$progress = "{$toRealName}已知晓，未处理";
			}
		}
		//	2 => '原始录入者认为申诉无效',
		if($status == 2){
			if($user['id'] == $toUserId){
				$progress = "我认为申诉无效，待{$createRealName}确认";
			}elseif($user['id'] == $createUserId){
				$progress = $toRealName."认为申诉无效，待我确认";
			}else{
				$progress = $toRealName."认为申诉无效，待{$createRealName}确认";
			}
		}	
		//	3 => '待市场总监处理',
		if($status == 3){
			$progress = "待市场总监处理";
		}
		//	4 => '市场总监设为无效',
		if($status == 4){
			$progress = "市场总监设为无效";
		}
		//	5 => '市场总监已解决',
		if($status == 5){
			$progress = "市场总监已解决";
		}
		//	6 => '原始录入者已解决',
		if($status == 6){
			if($user['id'] == $toUserId){
				$progress = "我已解决";
			}else{
				$progress = $toRealName."已解决";
			}
		}
		//	7 => '申诉者同意撤销冲突'
		if($status == 7){
			if($user['id'] == $createUserId){
				$progress = "我同意撤销冲突";
			}else{
				$progress = $createRealName."同意撤销冲突";
			}
		}
		return $progress;
	}
	
	/**
	 * 设置冲突记录状态
	 * 
	 * @param int $id	冲突记录id
	 * @param int $historyStatus	原状态
	 * @param int $newStatus		新状态
	 * @param int $content			备注
	 */
	public function setStatus($id, $historyStatus, $newStatus, $content =''){
		
		$res = D('salesDispute')->where(array('id'=>$id))->find();
		if(! $res){
			return FALSE;
		}
		
		$user = getUserSession();
		$createUser = D('admin')->where(array('id'=>$res['createUserId']))->find();
		$toUser = D('admin')->where(array('id'=>$res['toUserId']))->find();
		
		$data['historyStatus'] = $historyStatus;
		$data['newStatus'] = $newStatus;
		$data['content'] = $content;
		
		// 6 原始录入者已解决
		if($data['newStatus'] == 6 && $res['toUserId'] == $user['id'] && $createUser){
			//把IMEI转给申诉发起者
			D('sales')->where(array('id'=>$res['salesId']))->save(array('userId'=>$createUser['id'], 'storeId'=>$createUser['storeId']));
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = "原始录入者{$user['realName']}已解决";
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
		
		// 2 原始录入者认为申诉无效
		if($data['newStatus'] == 2 && $res['toUserId'] == $user['id']){
			
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = $content;
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
		
		// 3 待市场总监处理
		if($data['newStatus'] == 3 && $res['createUserId'] == $user['id']){
			
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = "申诉者{$user['realName']}不同意原始录入者{$toUser['realName']}所提出的申诉无效";
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
		
		// 7 申诉者同意撤销冲突
		if($data['newStatus'] == 7 && $res['createUserId'] == $user['id']){
			
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = "申诉者{$user['realName']}同意原始录入者{$toUser['realName']}所提出的申诉无效";
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
					
		//4 => '市场总监设为无效',
		if($data['newStatus'] == 4 && $user['roleId'] == 3){
			
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = "市场总监{$user['realName']}同意原始录入者{$toUser['realName']}所提出的申诉无效，理由：{$content}";
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
		
		//5 => '市场总监已解决',
		if($data['newStatus'] == 5 && $user['roleId'] == 3){
			//把IMEI转给申诉发起者
			D('sales')->where(array('id'=>$res['salesId']))->save(array('userId'=>$createUser['id'], 'storeId'=>$createUser['storeId']));
			
			// 则更改状态
			D("SalesDispute")->where(array('id'=>$id))->save(array('status'=>$newStatus));
			//记录 imei 申诉记录更改历史
			$history['disputeId'] = $id;
			$history['userId'] = $user['id'];
			$history['historyStatus'] = $historyStatus;
			$history['newStatus'] = $newStatus;
			$history['time'] = time();
			$history['content'] = "市场总监{$user['realName']}认为{$createUser['realName']}申诉有效，理由：{$content}";
			D("SalesDisputeHistory")->add($history);
			
			return TRUE;
		}
	}
	
	/**
	 * 获得冲突申诉系统提示信息
	 */
	public function getMsg(){
		$data = array();
		$needCount = 0;
		
		//0 => '待原录入者确认',
		//1 => '原始录入者已知晓，待处理',
		//3 => '待市场总监处理',
		//4 => '市场总监设为无效',
		//5 => '市场总监已解决',
		//6 => '原始录入者已解决',
		//7 => '申诉者同意撤销冲突',

		// 我创建的，待我处理的冲突记录
		//2 => '原始录入者认为申诉无效，待申诉者确认',
		$needCount += D('SalesDispute')->where(array('status'=>2, 'createUserId'=>  $this->user['id']))->count();

		//别人对我发起的冲突申诉
		//0 => '待原录入者确认',
		//1 => '原始录入者已知晓，待处理',
		$needCount += D('SalesDispute')->where(array('status'=>array('in','0,1'), 'toUserId'=>  $this->user['id']))->count();
	
		if($needCount > 0){
			$data['tag'] = 'salesDispute';
			$data['msg'] = "你有{$needCount}条冲突申诉信息需要处理！";
		}
		
		return $data;
	}
}

/* End of file salesDisputeModel.class.php */